package com.byxy.xyqas.controller;

import javax.servlet.http.HttpSession;

import com.byxy.xyqas.service.UserService;
import com.byxy.xyqas.vo.UserVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;

/**
 * UserController
 */
@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("getUserInfo.do")
	public String getUserInfo(Model model, @RequestParam("userId") Integer userId) {
		model.addAttribute("userObject", userService.get(userId));
		return "userInfo";
	}

	@RequestMapping("userSignUp.do")
	public String userSignUp(@RequestParam("user_name") String userName,
			@RequestParam("user_password") String userPassword, @RequestParam("user_nick") String userNick,
			@RequestParam("user_type") Integer userType, Model model) {
		UserVO userVO = new UserVO();
		userVO.setUserName(userName);
		userVO.setUserNick(userNick);
		userVO.setUserPassword(userPassword);
		userVO.setUserType(userType);
		userService.add(userVO);
		return "redirect:toLoginPage.do";
	}

	@RequestMapping("userLogin.do")
	public String userLogin(@RequestParam("userName") String username, @RequestParam("userPassword") String password,
			Model model, HttpSession session) {
		UserVO userVO = userService.login(username, password);
		if (userVO != null) {
			session.setAttribute("USER", userVO);
			return "redirect:toMainPage.do";
		} else {
			return "redirect:toLoginPage.do";
		}
	}

	@GetMapping("toLoginPage.do")
	public String toLoginPage() {
		return "login";
	}

	@GetMapping("toSignUpPage.do")
	public String toSignUpPage() {
		return "signUp";
	}

	@GetMapping("userSignOut.do")
	public String userSignOut(HttpSession session) {
		session.removeAttribute("USER");
		return "redirect:toMainPage.do";
	}

	@GetMapping("toUserManagePage.do")
	public String toUserManagePage(Model model) {
		model.addAttribute("users", userService.findAll());
		return "userManage";
	}

	@GetMapping("toUpdateUserInfoPage.do")
	public String toUpdateUserInfoPage(Model model, @RequestParam("userId") Integer userId) {
		model.addAttribute("userObject", userService.get(userId));
		return "update_userInfo";
	}

	@RequestMapping("updateUserInfo.do")
	public String updateUserInfo(Model model, @RequestParam("user_password") String userPassword,
			@RequestParam("user_nick") String userNick, @RequestParam("user_id") Integer userId) {
		UserVO userVO = userService.get(userId);
		userVO.setUserNick(userNick);
		userVO.setUserPassword(userPassword);
		userService.update(userVO);
		return "redirect:toUserManagePage.do";
	}

	@GetMapping("enableUser.do")
	public String enableUser(@RequestParam("userId") Integer userId) {
		userService.updateUserStatus(0, userId);
		return "redirect:toUserManagePage.do";
	}

	@GetMapping("lockUser.do")
	public String lockUser(@RequestParam("userId") Integer userId) {
		userService.updateUserStatus(2, userId);
		return "redirect:toUserManagePage.do";
	}

	@GetMapping("disableUser.do")
	public String disableUser(@RequestParam("userId") Integer userId) {
		userService.updateUserStatus(1, userId);
		return "redirect:toUserManagePage.do";
	}

}